/**
 * @version $Revision: 25 $
 * @class Ext.Updater.RemoteRenderer
 * @extends Ext.Updater.BasicRenderer
 * @author Andrew Stephanoff <andrew.stephanoff@gmail.com>
 *
 * Provide rendering component based on remote config. If scope of requeste is
 * instance of {@link Ext.Container} new component will be added into container
 * layout, otherwise ot will be added as child to base element
 */
Ext.namespace('Ext.Updater');
Ext.Updater.RemoteRenderer = Ext.extend(Ext.Updater.BasicRenderer, {
    /**
     * This is called when the transaction is completed and it's time to update
     * the element. RemoteRenderer add new ExtJS component to
     * {@link Ext.Container} or render it into {@link Ext.Element}.
     *
     * @param {Ext.Element} el The element being rendered
     * @param {Object} r The XMLHttpRequest object
     * @param {Updater} um The calling update manager
     * @param {Function} c A callback that will need to be called if loadScripts
     * is true on the Updater
     */
    render: function(el, r, um, c) {
        var ct  = r.argument.scope, cfg, s = false;

        try {
            cfg = Ext.decode(r.responseText);

        } catch (e) {
            if ('undefined' != typeof console) {
                console.log(e, r);
            }
            return;
        }

        // set default type
        if (!('xtype' in cfg)) {
            cfg.xtype = 'panel';
        }

        // clear element
        el.update('');

        try {
            // if container is instance of Ext.Container
            // add new component into layout
            if (ct instanceof Ext.Container) {
                ct.add(cfg);
                ct.doLayout();

            // otherwise render new component into element
            } else {
                cfg.renderTo = el;
                cmp = Ext.create(cfg);
            }
            s = true;

        } catch(e) {
            if ('undefined' != typeof console) {
                console.log(e, cfg, arguments);
            }
        }

        if (um.loadScripts && 'function' == typeof c) {
            c(el, s, r, cfg);
        }
    }
});